package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.HouseLeaseDAO;
import com.foreveross.dao.HouseLeaseDAO;
import com.foreveross.vo.HouseLease;

@Component("houseLeaseDAO")
public class HouseLeaseDAOImp extends SQLUtil implements HouseLeaseDAO {


	@Override
	public long findHouseLeaseListCount(String housingArea,String title,String houseId,
			String minMoney,String maxMoney,String text,String userId) throws Exception {
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append(" select count(*) from HouseLease model  ");
		sqlStr.append(" where 1=1 ");
		if(null!=housingArea && !"".equals(housingArea)){
			sqlStr.append(" and model.housingArea.codeId ='" + housingArea + "'");
		}
		if(null!=title && !"".equals(title)){
			sqlStr.append(" and model.title like '%" + title + "%'");
		}
		if(null!=houseId && !"".equals(houseId)){
			sqlStr.append(" and model.houseId ='" + houseId + "'");
		}
		
		
		if(null!=minMoney && !"".equals(minMoney)){
			sqlStr.append(" and model.rent>=" + minMoney);
		}
		if(null!=maxMoney && !"".equals(maxMoney)){
			sqlStr.append(" and model.rent <=" + maxMoney );
		}
		
		if(null!=text && !"".equals(text)){
			sqlStr.append(" and ( model.houseId.name like '%" + text + "%' or model.houseId.address like '%" + text + "%' ) ");
		}
		this.addAuthorityStr(sqlStr, userId, "houseId.id");
		return Long.parseLong(findObjectAll(sqlStr.toString()).get(0).toString()) ;
	}

	@Override
	public List<HouseLease> findHouseLeaseList(String housingArea,String title,String houseId,
			String minMoney,String maxMoney,String text,
			int pageIndex,int pageSize,String userId) throws Exception {
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append(" select model ");
		sqlStr.append(" from HouseLease model ");
		sqlStr.append(" where 1=1 ");
		if(null!=housingArea && !"".equals(housingArea)){
			sqlStr.append(" and model.housingArea.codeId ='" + housingArea + "'");
		}
		if(null!=title && !"".equals(title)){
			sqlStr.append(" and model.title like '%" + title + "%'");
		}
		if(null!=houseId && !"".equals(houseId)){
			sqlStr.append(" and model.houseId ='" + houseId + "'");
		}
		if(null!=minMoney && !"".equals(minMoney)){
			sqlStr.append(" and model.rent>=" + minMoney);
		}
		if(null!=maxMoney && !"".equals(maxMoney)){
			sqlStr.append(" and model.rent <=" + maxMoney );
		}
		if(null!=text && !"".equals(text)){
			sqlStr.append(" and ( model.houseId.name like '%" + text + "%' or model.houseId.address like '%" + text + "%' ) ");
		}
		this.addAuthorityStr(sqlStr, userId, "houseId.id");
		sqlStr.append(" order by model.top desc, model.creationTime desc");
		return (List<HouseLease>) findObjectAll(sqlStr.toString(), pageIndex,
				pageSize);
	}


}
